package com.yfp.client.service.impl.couponStrategy;

import cn.hutool.core.util.NumberUtil;
import com.yfp.client.domain.dto.ClientCouponBestDTO;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 百分比折扣券优惠券策略
 */
@Component("percentageDiscount")
public class PercentageDiscountClientCouponStrategy extends AbstractClientCouponStrategy {

    /**
     * 计算优惠后的金额
     * <p>
     * 10-2=8       计算后的值是8
     * 10*0.9=9     计算后的值是9
     *
     * @param priceOriginal
     * @param clientCoupons
     * @return
     */
    @Override
    public BigDecimal calculateDiscountedAfterPrice(BigDecimal priceOriginal, ClientCouponBestDTO clientCoupons) {
        // 9 折扣转换为 0.9
        BigDecimal discount = NumberUtil.div(clientCoupons.getDiscounts(), 10);

        BigDecimal price = NumberUtil.mul(priceOriginal, discount).setScale(2, BigDecimal.ROUND_DOWN);;
        if (NumberUtil.isLessOrEqual(price, BigDecimal.ZERO)) {
            return new BigDecimal("0.01");
        }
        return price;
    }

    /**
     * 计算优惠了的金额
     * <p>
     * 10-2=8       计算后的值是2
     * 10*0.9=9     计算后的值是1
     *
     * @param priceOriginal
     * @param clientCoupons
     * @return
     */
    @Override
    public BigDecimal calculateDiscountedPrice(BigDecimal priceOriginal, ClientCouponBestDTO clientCoupons) {
        // 9
        BigDecimal bigDecimal = this.calculateDiscountedAfterPrice(priceOriginal, clientCoupons);
        // 10-9=1
        return NumberUtil.sub(priceOriginal, bigDecimal);
    }
}
